package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.xuanzheng.company.entity.AttendanceSetting;
import com.xuanzheng.company.entity.EmployeePerformance;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.AttendanceSettingService;
import com.xuanzheng.company.service.EmployeePerformanceService;
import com.xuanzheng.company.service.impl.AttendanceSettingServiceImpl;
import com.xuanzheng.company.service.impl.EmployeePerformanceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.List;

@WebServlet("/api/user/performance")
public class GetUserPerformanceServlet extends HttpServlet {
    private EmployeePerformanceService employeePerformanceService = new EmployeePerformanceServiceImpl();
    private AttendanceSettingService attendanceSettingService = new AttendanceSettingServiceImpl();
    private Gson gson = new Gson();
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        JsonObject result = new JsonObject();
        
        // 获取当前登录用户
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        
        if (user == null) {
            result.addProperty("success", false);
            result.addProperty("message", "用户未登录");
            out.print(gson.toJson(result));
            return;
        }
        
        String companyId = user.getCompanyId();
        String userId = user.getId();
        
        if (companyId == null || companyId.isEmpty()) {
            result.addProperty("success", false);
            result.addProperty("message", "您尚未加入公司");
            out.print(gson.toJson(result));
            return;
        }
        
        try {
            // 获取绩点设置
            AttendanceSetting setting = attendanceSettingService.getByCompanyId(companyId);
            if (setting == null) {
                result.addProperty("success", false);
                result.addProperty("message", "公司尚未设置绩点参数");
                out.print(gson.toJson(result));
                return;
            }
            
            // 获取用户的所有绩点记录
            List<EmployeePerformance> performances = employeePerformanceService.getPerformancesByUserId(userId);
            
            result.addProperty("success", true);
            result.add("performances", gson.toJsonTree(performances));
            
            // 添加绩点设置信息
            JsonObject settingInfo = new JsonObject();
            settingInfo.addProperty("maxAttendanceHours", setting.getMaxAttendanceHours());
            settingInfo.addProperty("attendancePercentage", setting.getAttendancePercentage());
            settingInfo.addProperty("taskPercentage", setting.getTaskPercentage());
            result.add("settings", settingInfo);
            
            // 添加是否有记录的标志
            result.addProperty("hasRecord", !performances.isEmpty());
            
        } catch (Exception e) {
            e.printStackTrace();
            result.addProperty("success", false);
            result.addProperty("message", "获取绩点数据失败: " + e.getMessage());
        }
        
        out.print(gson.toJson(result));
    }
} 